package sysman.dao;

import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import org.springside.core.dao.HibernateEntityDao;
import org.hibernate.Query;

import sysman.model.OrderDealHistory;

import workflow.model.ProcessNode;

import common.PageInfo;

public class OrderDealDao extends HibernateEntityDao<OrderDealHistory>{
	public void save(OrderDealHistory po) {
		this.getHibernateTemplate().save(po);
	}
	
	public List<OrderDealHistory> findList(HashMap<String, String> hm, PageInfo pageInfo){
		try{
		
		String listHql = "FROM AppRecord po WHERE (1=1) ";
		String countHql = null;
		/*------------查询条件-------------*/
		//注意以设值方式组装查询条�?,参�?�PageItemManager 方法:findList
		
		String id = (String)hm.get("id");
		String taskId = (String)hm.get("taskId");
		String result = (String)hm.get("result");
		String reason = (String)hm.get("reason");
		String creator = (String)hm.get("creator");
		String createTime = (String)hm.get("createTime");


		int count[] = {0, 0, 0, 0, 0, 0};

		if(id != null && !"".equals(id.trim())){
			listHql += " AND id =:id";
			count[0] = 1;
		}

		if(taskId != null && !"".equals(taskId.trim())){
			listHql += " AND taskId =:taskId";
			count[1] = 1;
		}

		if(result != null && !"".equals(result.trim())){
			listHql += " AND result LIKE:result";
			count[2] = 1;
		}

		if(reason != null && !"".equals(reason.trim())){
			listHql += " AND reason LIKE:reason";
			count[3] = 1;
		}

		if(creator != null && !"".equals(creator.trim())){
			listHql += " AND creator =:creator";
			count[4] = 1;
		}

		if(createTime != null && !"".equals(createTime.trim())){
			listHql += " AND createTime =:createTime";
			count[5] = 1;
		}

		listHql += " ORDER BY po.id DESC";
		countHql = listHql;
		countHql = countHql.replace("FROM AppRecord", "SELECT COUNT(*) FROM AppRecord");
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);

		if(count[0] == 1){
			queryList.setLong("id", Long.valueOf(id));
			queryCount.setLong("id", Long.valueOf(id));
		}

		if(count[1] == 1){
			queryList.setLong("taskId", Long.valueOf(taskId));
			queryCount.setLong("taskId", Long.valueOf(taskId));
		}

		if(count[2] == 1){
			queryList.setString("result", "%" + result + "%");
			queryCount.setString("result", "%" + result + "%");
		}

		if(count[3] == 1){
			queryList.setString("reason", "%" + reason + "%");
			queryCount.setString("reason", "%" + reason + "%");
		}

		if(count[4] == 1){
			queryList.setLong("creator", Long.valueOf(creator));
			queryCount.setLong("creator", Long.valueOf(creator));
		}

		if(count[5] == 1){
			queryList.setDate("createTime", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(createTime));
			queryCount.setDate("createTime", (new java.text.SimpleDateFormat("yyyy-MM-dd")).parse(createTime));
		}



		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
		} catch (Exception e) {
			e.printStackTrace();
			return new ArrayList<OrderDealHistory>();
		}
	}
	
	public boolean deleteByIds(String ids){
		boolean isSuccessful = false;
		try{			
			String hql = "DELETE FROM AppRecord po WHERE po.id IN (" + ids + ")";
			this.getHibernateTemplate().bulkUpdate(hql);
			isSuccessful = true;
		} catch (Exception e){
			e.printStackTrace();
		}
		
		return isSuccessful;
	}
	
	public List<OrderDealHistory> getRecords(String taskId,PageInfo pageInfo)throws Exception{
		List<OrderDealHistory> records = null;
		
		String listHql = "select r from AppRecord r where r.taskId in (select t.id FROM " + ProcessNode.class.getName() + " p, Task t where t.proInsId=p.proInsId and p.id= "+taskId+") order by r.id desc";
		String countHql = "select count(r.id) from AppRecord r where r.taskId in (select t.id FROM " + ProcessNode.class.getName() + " p, Task t where t.proInsId=p.proInsId and p.id= "+taskId+") order by r.id desc";
		
		Query queryCount = this.createQuery(countHql);
		Query queryList = this.createQuery(listHql);
		//分页查询-START
		List countList = queryCount.list();
		int totalSize = 0;//总数
		if(countList.size() > 0){
			totalSize = Integer.valueOf(countList.get(0).toString());
			pageInfo.setTotalSize(totalSize);
		} else {
			pageInfo.setTotalSize(0);
		}
		
		queryList.setMaxResults(pageInfo.getPageSize());//设置查询数量
		queryList.setFirstResult(pageInfo.getPageNO() * pageInfo.getPageSize());//设置查询分段
		//分页查询-END
		
		return queryList.list();
		
	}
}
